package multithreadUDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.nio.ByteBuffer;

public class LembreteUDP extends Thread {
	private DatagramPacket pac;

	public LembreteUDP(DatagramPacket pack) {
		this.pac = pack;
	}

	public void run() {
		try {

			DatagramSocket s = new DatagramSocket();
			ByteBuffer bu = ByteBuffer.wrap(pac.getData());
			byte[] nome = new byte[20];
			bu.get(nome);
			String n = new String(nome);
			int intervalo = bu.getInt(20);
			int total = bu.getInt(24);

			for (int i = 0; i < total; i++) {

				String msg = "Tomar" + n;
				DatagramPacket pacEnviar = new DatagramPacket(msg.getBytes(),
						msg.getBytes().length, pac.getAddress(), pac.getPort());
				s.send(pacEnviar);
				System.out.println("impressao do n: "+n);

				Thread.sleep(intervalo * 1000);

			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
